#递归树工具类

from db import Db



class MyTree:
    #返回无限分类的层级结构
    def get_tree(self,data):
        #定义容器
        lists = []
        tree = {}
        #遍历数据
        for item in data:
            tree[item['id']] = item
        #手动断点
        # exit(-1)
        #遍历，判断是否具备父类
        for i in data:
            if not i['fid']:
                lists.append(tree[i['id']])
                #获取父级id 
            else:
                fid = i['fid']
                #一种有子集，一种没有
                if 'children' not in tree[fid]:
                    #生命子集key
                    tree[fid]['children'] = []
                #将子集插入到子集key里面
                tree[fid]['children'].append(tree[i['id']])
        return lists

# mytree = MyTree()
# print(mytree.get_tree([{'id':1,'fid':0},{'id':2,'fid':1},{'id':3,'fid':2}]))




